<template>
    <el-dialog :title="!dataForm.pkParameter ? '新增' : '修改'" :close-on-click-modal="false" destroy-on-close
        :visible.sync="visible" :before-close="closeDailog">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="120px">
            <el-form-item label="参数名称" prop="parameterName">
                <el-input v-model="dataForm.parameterName" placeholder="参数名称"></el-input>
            </el-form-item>
            <el-form-item label="参数key" prop="parameterKey">
                <el-input v-model="dataForm.parameterKey" placeholder="参数key"></el-input>
            </el-form-item>
            <el-form-item label="参数value" prop="parameterValue">
                <el-input v-model="dataForm.parameterValue" placeholder="参数value"></el-input>
            </el-form-item>
            <el-form-item label="参数code" prop="parameterCode">
                <el-input v-model="dataForm.parameterCode" placeholder="参数code"></el-input>
            </el-form-item>
            <el-form-item label="参数状态" prop="parameterStatus">
                <ec-dict-select v-model="dataForm.parameterStatus" placeholder="请选择参数状态" dictType="parameter_status">
                </ec-dict-select>
            </el-form-item>
            <el-form-item label="参数类型" prop="parameterType">
                <ec-dict-select v-model="dataForm.parameterType" placeholder="请选择参数类型" dictType="parameter_type">
                </ec-dict-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="closeDailog">取消</el-button>
            <el-button type="primary" :loading="submitLoading" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
    import * as parameterService from '@/api/bdms/parameter'
    export default {
        data() {
            return {
                visible: false,
                submitLoading: false,
                dataForm: {
                    pkParameter: 0,
                    parameterName: '',
                    parameterKey: '',
                    parameterValue: '',
                    parameterCode: '',
                    parameterStatus: '',
                    parameterType: '',
                    revision: '',
                },
                dataRule: {
                    parameterName: [{
                        required: true,
                        message: '参数名称不能为空',
                        trigger: 'blur'
                    }],
                    parameterKey: [{
                        required: true,
                        message: '参数key不能为空',
                        trigger: 'blur'
                    }],
                    parameterValue: [{
                        required: true,
                        message: '参数value不能为空',
                        trigger: 'blur'
                    }],
                    parameterCode: [{
                        required: true,
                        message: '参数code不能为空',
                        trigger: 'blur'
                    }],
                    parameterStatus: [{
                        required: true,
                        message: '参数状态不能为空',
                        trigger: 'change'
                    }],
                    parameterType: [{
                        required: true,
                        message: '参数类型不能为空',
                        trigger: 'change'
                    }],
                }
            }
        },
        methods: {
            init(id) {
                this.dataForm.pkParameter = id || 0
                this.visible = true
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.pkParameter) {
                        parameterService.getparameterinfo(this.dataForm.pkParameter).then(data => {
                            this.dataForm.parameterName = data.parameterName
                            this.dataForm.parameterKey = data.parameterKey
                            this.dataForm.parameterValue = data.parameterValue
                            this.dataForm.parameterCode = data.parameterCode
                            this.dataForm.parameterStatus = data.parameterStatus
                            this.dataForm.parameterType = data.parameterType
                            this.dataForm.revision = data.revision
                        })
                    }
                })
            },
            // 表单提交
            dataFormSubmit() {
                this.$refs['dataForm'].validate((valid) => {
                    if (valid) {
                        this.submitLoading = true
                        var params = {
                            'pkParameter': this.dataForm.pkParameter || undefined,
                            'parameterName': this.dataForm.parameterName,
                            'parameterKey': this.dataForm.parameterKey,
                            'parameterValue': this.dataForm.parameterValue,
                            'parameterCode': this.dataForm.parameterCode,
                            'parameterStatus': this.dataForm.parameterStatus,
                            'parameterType': this.dataForm.parameterType,
                            'revision': this.dataForm.revision,
                        }
                        var tick = !this.dataForm.pkParameter ? parameterService.addparameter(params) :
                            parameterService.updateparameter(params)
                        tick.then(data => {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.submitLoading = false
                                    this.closeDailog()
                                    this.$emit('refreshDataList')
                                }
                            })
                        }).catch((res) => {
                            this.submitLoading = false
                            this.closeDailog()
                        })
                    }
                })
            },
            closeDailog() {
                this.$refs['dataForm'].resetFields()
                this.visible = false
            },
        }
    }
</script>